package com.ruoyi.system.utils;

import java.util.Random;

public class RandomUtil {
    public  static String randomStr(int length){
        String result = "";
        Random random = new Random();
        for ( int i = 0; i < length; i++ )
        {
            String str = random.nextInt( 2 ) % 2 == 0 ? "num" : "char";
            if ( "char".equalsIgnoreCase( str ) )
            { // 产生字母
                int nextInt = random.nextInt( 2 ) % 2 == 0 ? 65 : 97;
                // System.out.println(nextInt + "!!!!"); 1,0,1,1,1,0,0
                result += (char) ( nextInt + random.nextInt( 26 ) );
            }
            else if ( "num".equalsIgnoreCase( str ) )
            { // 产生数字
                result += String.valueOf( random.nextInt( 10 ) );
            }
        }

        return result;
    }

    /**
     * 获取随机生成验证码
     * @param codeCount
     * @return
     */
    public static String generateVerifyCode(int codeCount) {
        String code = "";
        int r=0;
      if(codeCount>0) {
          int a=1;
          String str = String.format("%0"+codeCount+"d", a);
          a=Integer.valueOf(a+""+str.substring(0,str.length()-1));
          r = (int) ((Math.random() * 9 + 1) * a);
          code=""+r;
      }
        return code;
    }
    public static String getVerifyCode(int length) {
        String result = "";
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            result += String.valueOf(random.nextInt(10));
        }
        return result;
    }
}
